﻿using AiXiu.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AiXiu.Model;
using AiXiu.BLL;

namespace AiXiu.WebSite
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //防止页面刷新后页面上的数据消失
            this.txtUserName.Attributes["value"] = txtUserName.Text;
            this.txtPassword.Attributes["value"] = txtPassword.Text;
            this.txtUserPhone.Attributes["value"] = txtUserPhone.Text;
        }
        /// <summary>
        /// 注册按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            //判断是否通过表单验证
            if (Page.IsValid)
            {
                //获取验证码
                string Code = Session["image"].ToString().Trim();
                string Txtbox = this.txtCaptcha.Text.ToString().Trim();
                //判断验证码是否为空
                if (Txtbox == "")
                {
                    this.Alert("codeError1", "验证码不能为空");
                }
                else
                {
                    //判断用户输入的验证码是否正确
                    if (Code != Txtbox.ToUpper())
                    {
                        this.Alert("codeError2", "输入的验证码有误");
                        txtCaptcha.Text = "";
                    }
                    else
                    {
                        //获取页面用户输入的信息(用户名,手机号,密码)
                        TBLogins login = new TBLogins();
                        login.UserName = txtUserName.Text;
                        login.MobileNumber = txtUserPhone.Text;
                        login.Password = txtPassword.Text;

                        //调用LoginBLL中的注册方法
                        LoginBLL loginBLL = new LoginBLL();
                        OperResult result = loginBLL.Register(login);
                        if (result.StatusCode == StatusCode.Succeed)
                        {
                            //注册成功弹窗并跳转到登录页面
                            this.AlertAndRedirect("regSuccess","注册成功！","Login.aspx");                          
                        }
                        else
                        {
                            this.Alert("regerr", result.Message);                          
                        }
                    }
                }
            }
        }
    }
}